package com.xbf.controller;

import com.xbf.entity.Admin;
import com.xbf.entity.Order;
import com.xbf.entity.User;
import com.xbf.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam String username, @RequestParam String password) {
        Map<String, Object> result = new HashMap<>();
        Admin admin = adminService.login(username, password);

        if (admin != null) {
            result.put("success", true);
            result.put("message", "登录成功");
            result.put("admin", admin);
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }

        return result;
    }

    @GetMapping("/users")
    public List<User> getAllUsers() {
        return adminService.getAllUsers();
    }

//    @GetMapping("/orders")
//    public List<Order> getAllOrders() {
//        return adminService.getAllOrders();
//    }

    @GetMapping("/orders")
    public Map<String, Object> getAllOrders() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Order> orders = adminService.getAllOrders();
            result.put("success", true);
            result.put("data", orders);
            result.put("total", orders.size());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取订单失败");
        }
        return result;
    }

}